// src/stores/merchant.js
import { defineStore } from 'pinia'
import {
  listAllMerchantsAPI,
  addMerchantAPI,
  updateMerchantAPI,
  deleteMerchantAPI,
  getMerchantAPI,
  toggleMerchantStatusAPI,
  searchMerchantByNameAPI
} from '@/api/merchantAPI'

export const useMerchantStore = defineStore('merchant', {
  state: () => ({
    merchants: [],
    total: 0,
    currentPage: 1,
    pageSize: 10,
    loading: false,
    selectedMerchant: null
  }),

  actions: {
    // 获取所有商户
    async fetchMerchants() {
      this.loading = true
      try {
        const res = await listAllMerchantsAPI()
        
        if (res && (res.code === 0 || res.code === 200) && res.data) {
          // 后端返回的是 List<MerchantInfoDTO>
          if (Array.isArray(res.data)) {
            this.merchants = res.data
            this.total = res.data.length
          } else {
            this.merchants = []
            this.total = 0
          }
        } else {
          console.error('获取商户列表失败:', res?.message || '未知错误')
          this.merchants = []
          this.total = 0
        }
      } catch (error) {
        console.error('获取商户失败:', error)
        this.merchants = []
        this.total = 0
      } finally {
        this.loading = false
      }
    },

    // 根据商户名称搜索商户
    async searchMerchantsByName(name) {
      this.loading = true
      try {
        const res = await searchMerchantByNameAPI(name)

        if (res && (res.code === 0 || res.code === 200) && res.data) {
          if (Array.isArray(res.data)) {
            this.merchants = res.data
            this.total = res.data.length
          } else {
            this.merchants = []
            this.total = 0
          }
        } else {
          console.error('搜索商户失败:', res?.message || '未知错误')
          this.merchants = []
          this.total = 0
        }
      } catch (error) {
        console.error('搜索商户失败:', error)
        this.merchants = []
        this.total = 0
      } finally {
        this.loading = false
      }
    },

    // 新增商户
    async addMerchant(merchantData) {
      try {
        const res = await addMerchantAPI(merchantData)
        if (res && (res.code === 0 || res.code === 200)) {
          await this.fetchMerchants()
          return { success: true }
        } else {
          throw new Error(res?.message || '添加商户失败')
        }
      } catch (error) {
        console.error('新增商户失败:', error)
        throw error
      }
    },

    // 编辑商户
    async updateMerchant(id, merchantData) {
      try {
        const res = await updateMerchantAPI(id, merchantData)
        if (res && (res.code === 0 || res.code === 200)) {
          await this.fetchMerchants()
          return { success: true }
        } else {
          throw new Error(res?.message || '更新商户失败')
        }
      } catch (error) {
        console.error('更新商户失败:', error)
        throw error
      }
    },

    // 删除商户
    async deleteMerchant(id) {
      try {
        const res = await deleteMerchantAPI(id)
        if (res && (res.code === 0 || res.code === 200)) {
          await this.fetchMerchants()
          return { success: true }
        } else {
          throw new Error(res?.message || '删除商户失败')
        }
      } catch (error) {
        console.error('删除商户失败:', error)
        throw error
      }
    },

    // 获取单个商户详情
    async getMerchantById(id) {
      try {
        const res = await getMerchantAPI(id)
        if (res && (res.code === 0 || res.code === 200) && res.data) {
          this.selectedMerchant = res.data
          return res.data
        } else {
          throw new Error(res?.message || '获取商户详情失败')
        }
      } catch (error) {
        console.error('获取商户详情失败:', error)
        throw error
      }
    },

    // 切换商户状态
    async toggleMerchantStatus(id) {
      try {
        const res = await toggleMerchantStatusAPI(id)
        if (res && (res.code === 0 || res.code === 200)) {
          await this.fetchMerchants()
          return { success: true }
        } else {
          throw new Error(res?.message || '切换商户状态失败')
        }
      } catch (error) {
        console.error('切换商户状态失败:', error)
        throw error
      }
    }
  }
})